#!/usr/bin/perl -w 
# Check status of the Amazon Web Services
# 06/2008, Vlastimil Holer <vlastimil.holer@gmail.com>, GNU/GPL2

use XML::RSS;
use LWP::Simple;

my @feeds=(
	{	key		=> 'EC2API',
		desc	=> 'Amazon Elastic Compute Cloud (API)',
		url		=> 'http://status.aws.amazon.com/rss/EC2API.rss' },
	{	key		=> 'EC2',
		desc	=> 'Amazon Elastic Compute Cloud (Instances)',
		url		=> 'http://status.aws.amazon.com/rss/EC2.rss' },
	{	key		=> 'FPS',
		desc	=> 'Amazon Flexible Payments Service',
		url		=> 'http://status.aws.amazon.com/rss/FPS.rss' },
	{	key		=> 'MTURKR',
		desc	=> 'Amazon Mechanical Turk (Requester)',
		url		=> 'http://status.aws.amazon.com/rss/MTURKR.rss' },
	{	key		=> 'MTURKW',
		desc	=> 'Amazon Mechanical Turk (Worker)',
		url		=> 'http://status.aws.amazon.com/rss/MTURKW.rss' },
	{	key		=> 'SDB',
		desc	=> 'Amazon SimpleDB',
		url		=> 'http://status.aws.amazon.com/rss/SDB.rss' },
	{	key		=> 'S3EU',
		desc	=> 'Amazon Simple Storage Service (EU)',
		url		=> 'http://status.aws.amazon.com/rss/S3EU.rss' },
	{	key		=> 'S3US',
		desc	=> 'Amazon Simple Storage Service (US)',
		url		=> 'http://status.aws.amazon.com/rss/S3US.rss' },
	{	key		=> 'SQS',
		desc	=> 'Amazon Simple Queue Service',
		url		=> 'http://status.aws.amazon.com/rss/SQS.rss' }
);

my $rtn='';
my @srv_error;
my @feeds_only=@ARGV;

for my $feed (@feeds) {
	if (scalar(@feeds_only)) {
		my $accept=0;
		for my $test_feed (@feeds_only) {
			$accept=1 if $feed->{'key'} eq $test_feed;
		}
		next unless $accept;
	}

	my $content=get($feed->{'url'});
	my $rss=XML::RSS->new();
	$rss->parse($content);

	# there is some message for service
	if (scalar(@{$rss->{'items'}})) {
		push(@srv_error,$feed->{'key'});
		$rtn.=sprintf("Problem with '%s':\n",$feed->{'desc'});
		for my $item (@{$rss->{'items'}}) {
			$rtn.='Title       : '.$item->{'title'}."\n"
				if $item->{'title'};
			$rtn.='Description : '.$item->{'description'}."\n"
				if $item->{'description'};
		}
	}
}

if ($rtn) {
	print(join(' ',@srv_error)."\n");
	print($rtn);
	exit(1);
} else {
	exit(0);
}
